package edu.sandau.basic.service.impl;

import edu.sandau.basic.domain.Role;
import edu.sandau.basic.domain.User;
import edu.sandau.basic.mapper.UserRoleMapper;
import edu.sandau.basic.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserRoleServiceImpl implements UserRoleService {

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public List<Role> getUserRoles(Integer userId) {
        return userRoleMapper.selectRolesByUserId(userId);
    }

    @Override
    @Transactional
    public void updateUserRoles(Integer userId, List<Integer> roleIds) {
        userRoleMapper.deleteUserRoles(userId);
        if (roleIds != null && !roleIds.isEmpty()) {
            userRoleMapper.insertUserRoles(userId, roleIds);
        }
    }

    @Override
    @Transactional
    public void deleteUserRoles(Integer userId) {
        userRoleMapper.deleteUserRoles(userId);
    }

    @Override
    public List<User> getUsersByRole(Integer roleId) {
        return userRoleMapper.selectUsersByRoleId(roleId);
    }
}
